package likouJava;

public class 二叉树的直径 {

    private int maxDiameter = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) return 0;

        //长度 = 左边深度 + 右边深度
        maxDepth(root);
        return maxDiameter;

    }
    public int maxDepth(TreeNode root) {
        if(root == null)return 0;

        int left = maxDepth(root.left);
        int right = maxDepth(root.right);

        maxDiameter = Math.max(maxDiameter, left + right);
        return 1 + Math.max(left, right);
    }

}
